package threadcommunication.procucerconsumer.demo_04;

import java.util.ArrayList;
import java.util.List;

public class MyStack {
    private List list = new ArrayList<>();

    synchronized public void push() {
        try {
            while(list.size() == 1) {
                this.wait();
            }

            list.add("anything=" + Math.random());
            this.notifyAll();
            System.out.println("push=" + list.size());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    synchronized public String pop() {
        String returnValue = "";
        try {
            while(list.size() == 0) {
                System.out.println("pop操作中的:" + Thread.currentThread().getName() + "is Waitting");
                this.wait();
            }

            returnValue = "" + list.get(0);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        return returnValue;
    }
}
